<?php 

// Davide.Gustin by SpheraIT

if (extension_loaded('gettext')) {
	if (isset($_COOKIE['lang'])) {
		setlocale(LC_MESSAGES,  $_COOKIE['lang']);
//    setcookie("lang",  $_COOKIE['lang'], time()+2592000);
  } else {
			setlocale(LC_MESSAGES,  'en_US');
	}
	bindtextdomain('main','../../i18n');
  textdomain('main');
}

function file_parser_snmpd($filename) {
		$file = file($filename);
		foreach ($file as $line) {
			if (preg_match("/^\s*([a-zA-Z0-9]+)\s*\s*(.*)\s*([;#].*)?/",$line,$matches)) {
				$conf[ $matches[1] ] = $matches[2];
			}
		}
		return $conf;
}

$action = $_REQUEST['action'];
    
if ($action == 'save') {
	
	$restrictip = $_REQUEST['restrictip'];
	$sysrocommunity = $_REQUEST['sysrocommunity'];
	$sysdescr = $_REQUEST['sysdescr'];
	$syslocation = $_REQUEST['syslocation'];
	$syscontact = $_REQUEST['syscontact'];
	$sysname = $_REQUEST['sysname'];

	exec("sudo /var/www/html/private/snmpdconfig/snmpdconfig.sh \"$sysdescr\" \"$syslocation\" \"$syscontact\" \"$sysname\" \"$sysrocommunity\" \"$restrictip\"");

	unset($restrictip);
	unset($sysrocommunity);
	unset($sysdescr);
	unset($syslocation);
	unset($syscontact);
	unset($sysname);
	
}

    $parsersnmpd = file_parser_snmpd("/etc/snmp/snmpd.conf");

    $sysrocommunity = explode(' ', $parsersnmpd["rocommunity"]);
    $sysdescr = $parsersnmpd["sysdescr"];
    $syslocation = $parsersnmpd["syslocation"];
    $syscontact = $parsersnmpd["syscontact"];
    $sysname = $parsersnmpd["sysname"];    

?>

<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<link href="mainstyle.css" rel="stylesheet" type="text/css">
<title>Voiper snmpd WebConfig</title>

<SCRIPT language="javascript" type="text/javascript">
<!--

var errfound = false;

function error(elem, text) {
	if (errfound) return;
	window.alert(text);
	elem.select();
	elem.focus();
	errfound = true;
}

function isValidIPAddress(form,ipaddr) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { error(form,"<?php echo _("The IP Address is not corrected.\\nPlease write a correct ip Address. (format: nnn.nnn.nnn.nnn where nnn is a number from 0 to 255)")?>"); }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) { error(form,"<?php echo _("The IP Address is not corrected.\\nPlease write a correct ip Address. (format: nnn.nnn.nnn.nnn where nnn is a number from 0 to 255)")?>"); }
      }
   } else {
      error(form,"<?php echo _("The IP Address is not corrected.\\nPlease write a correct ip Address. (format: nnn.nnn.nnn.nnn where nnn is a number from 0 to 255)")?>");
   }
}

function snmpd_start(f2) {

	cancel = false;
	ok = true;

	if (confirm("<?php echo _("Are you sure to apply and save the new snmp Configuration?")?>"))
  		return ! cancel;
	else
  		return ! ok;
}

function NetCheck(form) {

	errfound = false;

	if (form.restrictip.value != "") {
        isValidIPAddress(form.restrictip,form.restrictip.value);
    }

	if (form.sysrocommunity.value == "") {
	   error(form.sysrocommunity,"<?php echo _("Please type the snmp Community.")?>");
       form.sysrocommunity.focus();
    }


  	return ! errfound;
}

// -->
</SCRIPT>

</HEAD>

<BODY text="#000000" vLink="#000000" aLink="#000000" link="#000000" bgColor="#eeeeee" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form name="network" action="snmpdconfig.php" method="post" onSubmit="return NetCheck(this);">
<input type="hidden" name="action" value="save"/>

<table width="100%" border="0" cellspacing="3" cellpadding="0">
    <tr>
    <td colspan="2">&nbsp;</td>
    </tr>

	<tr>
    <td width="55%"><b><?php echo _("Snmp Name")?>:</b></td>
    <td width="45%"><input name="sysname" type="text" id="sysname" value="<?php echo $sysname?>" size="35" maxlength="50"></td>
	</tr>

	<tr>
    <td width="55%"><b><?php echo _("Snmp Description")?>:</b></td>
    <td width="45%"><input name="sysdescr" type="text" id="sysdescr" value="<?php echo $sysdescr?>" size="35" maxlength="50"></td>
	</tr>

	<tr>
    <td width="55%"><b><?php echo _("Snmp Location")?>:</b></td>
    <td width="45%"><input name="syslocation" type="text" id="syslocation" value="<?php echo $syslocation?>" size="35" maxlength="50"></td>
	</tr>

	<tr>
    <td width="55%"><b><?php echo _("Snmp Contact")?>:</b></td>
    <td width="45%"><input name="syscontact" type="text" id="syscontact" value="<?php echo $syscontact?>" size="35" maxlength="50"></td>
	</tr>

	<tr>
    <td width="55%"><b><?php echo _("ROcommunity")?>:</b></td>
    <td width="45%"><input name="sysrocommunity" type="text" id="sysrocommunity" value="<?php echo $sysrocommunity[0]?>" size="35" maxlength="50"></td>
	</tr>

	<tr>
    <td width="55%"><b><?php echo _("Restrict IP")?>:</b></td>
    <td width="45%"><input name="restrictip" type="text" id="restrictip" value="<?php echo $sysrocommunity[1]?>" size="35" maxlength="50"></td>
	</tr>

	<tr>
	<td colspan="2"><div align="right"><input name="Submit" type="submit" value="Salva" onClick="return snmpd_start(this);"></div></td>
	<tr>
	<td colspan="2">* <?php echo _("For advance-configuration please use Webmin.")?></td>
	</tr>
	</tr>

  </table>
</form>
</BODY></HTML>
